package com.wedu.modules.dss.controller;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.R;
import com.wedu.common.validator.ValidatorUtils;
import com.wedu.modules.dss.dao.DssDeviceInformationDao;
import com.wedu.modules.dss.entity.dto.breakDownDTO;
import com.wedu.modules.dss.entity.DssDeviceInformationEntity;
import com.wedu.modules.dss.entity.DssOperationStatusEntity;
import com.wedu.modules.dss.entity.DssRepairManageEntity;
import com.wedu.modules.dss.entity.dto.breakDownDTO;
import com.wedu.modules.dss.service.DssRepairManageService;
import com.wedu.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/dss/operator")
public class DssRepairManageController extends AbstractController {
    @Autowired
    private DssRepairManageService dssRepairManageService;
    @Autowired
    DssDeviceInformationDao dssDeviceInformationDao;
    /**
     * 红灯预警列表展示
     * @param params
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("dss:operator:list")
    public R list(@RequestParam Map<String,Object> params){
        Long userId = getUserId();
        PageUtils page=dssRepairManageService.queryPage(params, userId);
        Integer isOperator = getUser().getIsOperator();
        MPJLambdaWrapper<DssDeviceInformationEntity> wrapper2 = new MPJLambdaWrapper<DssDeviceInformationEntity>()
                .select(DssDeviceInformationEntity::getDeviceId, DssDeviceInformationEntity::getDeviceName,
                        DssDeviceInformationEntity::getDeviceModel,DssDeviceInformationEntity::getId)
                .select(DssOperationStatusEntity::getLightStart, DssOperationStatusEntity::getLightOver,
                        DssOperationStatusEntity::getLightStatus)
                .select(DssRepairManageEntity::getRepairStatus,DssRepairManageEntity::getBreakdownCode)
                .leftJoin(DssOperationStatusEntity.class, DssOperationStatusEntity::getDeviceId, DssDeviceInformationEntity::getId)
                .leftJoin(DssRepairManageEntity.class, DssRepairManageEntity::getDeviceId, DssDeviceInformationEntity::getId)
//                .like(StringUtils.isNotBlank(deviceNumber), DssDeviceInformationEntity::getDeviceId, deviceNumber)//模糊查询
                .eq(DssOperationStatusEntity::getLightStatus, -1)//红灯列表中的设备维修状态都是待维修的,所以将维修状态都默认设置为 0
                .isNull(DssOperationStatusEntity::getLightOver)
                .eq(DssRepairManageEntity::getRepairStatus,0);//维修工人界面展示维修中数据据
//                .isNotNull(DssRepairManageEntity::getBreakdownCode);
        List<breakDownDTO> breakDownDTOList2 = dssDeviceInformationDao.selectJoinList(breakDownDTO.class, wrapper2);
        Set<Map<String,Object>> deviceSet=new HashSet<>();
        for (breakDownDTO dto : breakDownDTOList2) {
            Map<String,Object> deviceMap=new HashMap<>();
            deviceMap.put("deviceNumberList",dto.getDeviceId());
            deviceSet.add(deviceMap);
        }
        R r=R.ok();
        r.put("isOperator",isOperator);
        r.put("page",page);
        r.put("deviceNumberList",deviceSet);
        return r;
    }

    /**
     * 申请维修后的维修工人列表
     * @param ids
     * @return
     */
    @PostMapping("/apply")
    @RequiresPermissions("dss:operator:apply")
    public R apply(@RequestBody int[] ids){
        // 调用业务逻辑层的方法进行更新操作，返回更新后的设备列表
        dssRepairManageService.apply(ids);
        // 返回业务逻辑层处理后的响应
        return R.ok();
    }


    /**
     * 返回数据库中的信息到前端
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("dss:operator:info")
    public R info(@PathVariable("id") Long deviceId){
        DssRepairManageEntity dssRepairManageEntity=dssRepairManageService.getByDeviceId(deviceId);
        return R.ok().put("dssRepairManageEntity",dssRepairManageEntity);
    }

    /**
     * 开始维修
     */
    @RequestMapping("/save")
    @RequiresPermissions("dss:operator:save")
    public R save(@RequestBody DssRepairManageEntity dssRepairManageEntity){
        ValidatorUtils.validateEntity(dssRepairManageEntity);
//        dssRepairManageEntity.setRepairStarttime(new Date());
//        Long userId = getUser().getUserId();
//        dssRepairManageEntity.setUserId(userId);
        R r = dssRepairManageService.saveMsg(dssRepairManageEntity,getUserId());
        return r;
    }

    /**
     * 完成按钮
     */
    @RequestMapping("/completion")
    @RequiresPermissions("dss:operator:completion")
    public R completion(@RequestBody int Id){
        //维修结束时间和创建时间,创建者，把维修状态改为正常1
        R r = dssRepairManageService.complete(Id,getUserId());
        return r;
    }

    /**
     * 展示故障库信息
     */
    @RequestMapping("/manageList")
    @RequiresPermissions("dss:operator:manageList")
    public R manageList(@RequestParam Map<String,Object> params){
        PageUtils page=dssRepairManageService.queryManagePage(params,getUserId());
        Integer isOperator = getUser().getIsOperator();
        R r=R.ok();
        r.put("isOperator",isOperator);
        r.put("page",page);
        return r;
    }

    /**
     * 返回故障库中的信息到前端
     */
    @GetMapping("/Manageinfo/{id}")
    @RequiresPermissions("dss:operator:Manageinfo")
    public R Manageinfo(@PathVariable("id") int id){
        DssRepairManageEntity dssRepairManageEntity=dssRepairManageService.getById(id);
        return R.ok().put("dssRepairManageEntity",dssRepairManageEntity);
    }

    /**
     * 修改故障库中的某条信息
     */
    @RequestMapping("/update")
    @RequiresPermissions("dss:operator:update")
    public R update(@RequestBody DssRepairManageEntity dssRepairManageEntity){
        ValidatorUtils.validateEntity(dssRepairManageEntity);
        R r = dssRepairManageService.updateMsg(dssRepairManageEntity,getUserId());
        return r;
    }

    /**
     * 删除一条或者多条数据
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dss:operator:delete")
    public R delete(@RequestBody Long[] ids){
        dssRepairManageService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 查找已经确认故障码的列表
     */
    @RequestMapping("/seekCode")
    @RequiresPermissions("dss:operator:seekCode")
    public R seekCode(@RequestParam Map<String,Object> params){
        R r=dssRepairManageService.hasCodeNumber(params);
        return r;
    }


    /**
     * 查找待诊断的列表
     */
    @RequestMapping("/waitConfirm")
    @RequiresPermissions("dss:operator:waitConfirm")
    public R waitConfirm(@RequestParam Map<String,Object> params){
        R r=dssRepairManageService.NoCodeNumber(params);
        return r;
    }


    /**
     *
     */
    @RequestMapping("/api")
    @RequiresPermissions("dss:operator:api")
    public R getDeviceFaultStat(@RequestParam("deviceId2") String deviceId2) {
//        List<DeviceFaultStatVO> data = dssRepairManageService.getFaultStatistics(deviceId2);
        R r = dssRepairManageService.getFaultStatistics(deviceId2);
//        return R.ok().put("list",data);
        return r;
    }

}
